package dao;

import entity.FeeType;
import util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FeeTypeDao {

    public List<FeeType> getAllFeeTypes() {
        String sql = "SELECT * FROM fee_type ORDER BY id";
        List<FeeType> feeTypes = new ArrayList<>();

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql);
                ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                FeeType feeType = new FeeType();
                feeType.setId(rs.getInt("id"));
                feeType.setName(rs.getString("name"));
                feeType.setPrice(rs.getBigDecimal("price"));
                feeType.setUnit(rs.getString("unit"));
                feeType.setDescription(rs.getString("description"));
                feeType.setStatus(rs.getInt("status"));
                feeType.setCreateTime(rs.getTimestamp("create_time"));
                feeTypes.add(feeType);
            }
        } catch (Exception e) {
            throw new RuntimeException("获取费用类型列表失败", e);
        }
        return feeTypes;
    }

    public void addFeeType(FeeType feeType) {
        String sql = "INSERT INTO fee_type (name, price, unit, description, status) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, feeType.getName());
            stmt.setBigDecimal(2, feeType.getPrice());
            stmt.setString(3, feeType.getUnit());
            stmt.setString(4, feeType.getDescription());
            stmt.setInt(5, feeType.getStatus());
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("添加费用类型失败", e);
        }
    }

    public void updateFeeType(FeeType feeType) {
        String sql = "UPDATE fee_type SET name = ?, price = ?, unit = ?, description = ?, status = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, feeType.getName());
            stmt.setBigDecimal(2, feeType.getPrice());
            stmt.setString(3, feeType.getUnit());
            stmt.setString(4, feeType.getDescription());
            stmt.setInt(5, feeType.getStatus());
            stmt.setInt(6, feeType.getId());
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("更新费用类型失败", e);
        }
    }

    public void deleteFeeType(int id) {
        String sql = "DELETE FROM fee_type WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("删除费用类型失败", e);
        }
    }

    public FeeType getFeeTypeById(int id) {
        String sql = "SELECT * FROM fee_type WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    FeeType feeType = new FeeType();
                    feeType.setId(rs.getInt("id"));
                    feeType.setName(rs.getString("name"));
                    feeType.setPrice(rs.getBigDecimal("price"));
                    feeType.setUnit(rs.getString("unit"));
                    feeType.setDescription(rs.getString("description"));
                    feeType.setStatus(rs.getInt("status"));
                    feeType.setCreateTime(rs.getTimestamp("create_time"));
                    return feeType;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("获取费用类型失败", e);
        }
        return null;
    }
}